//
//  NetworkClient.m
//  Lesson
//
//  Created by centling on 15/4/21.
//  Copyright (c) 2015年 Seas.CHENG. All rights reserved.
//

#import "NetworkClient.h"
#import "Global.h"
#import "Mantle.h"

@implementation NetworkClient

+(NetworkClient *)intance{
    static NetworkClient *networkClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        networkClient=[[NetworkClient alloc] init];
    });
    return networkClient;
}


-(id)init{
//    return [super init];
    NSString *baseURLStr=@"http://api.map.baidu.com/telematics/v3/";
    NSURL *baseURL=[NSURL URLWithString:baseURLStr];
    return [super initWithBaseURL:baseURL];
}



-(void)getWeather:(NSString *)city completionBlock:(void(^)(Weather *weather, NSError *error))block{

    NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSDictionary *dic=[[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *bundleIndentifier=[dic objectForKey:@"CFBundleIdentifier"];
    
    NSString *URLStr=[NSString stringWithFormat:@"weather?location=%@&output=json&ak=%@&mcode=%@",city,baiduAppID,bundleIndentifier];
    
    NSString *encodeURL=[URLStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [self GET:encodeURL parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        Weather *weather=[MTLJSONAdapter modelOfClass:[Weather class] fromJSONDictionary:responseObject error:nil];
        
        
        
        block(weather,nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
         block(nil,error);
    }];
}



@end
